---@class RedRob.SkillNode
---@field New fun() : RedRob.SkillNode
local SkillNode = RedRob.CreateClass("SkillNode")

---@param skill RedRob.Skill
---@return RedRob.SkillNode
function SkillNode.CreateNode(skill,nodeType,actionName,resName,posX,posY,bone)
    local node = SkillNode.New()
    node:OnCreate(skill,nodeType)
    node.action = actionName
    node.resName = resName
    node.offsetX = posX or 0
    node.offsetY = posY or 0
    node.bone = bone
    return node
end
function SkillNode:OnCreate(skill,nodeType)
    self.nodeType = nodeType
    ---@type RedRob.Skill
    self.skill = skill
    self.bulletId = 0
    self.resName = ""   --资源名
    self.offsetX = 0   --偏移量
    self.offsetY = 0
    self.isLeft = false --左右
    self.action = nil   --动作名

    self.isExecute = false  --是否已执行
    self.delayTime = -1
    self.during = -1
    self.curDelayTime = 0
    self.time = 0
    self.bone = nil     --参考的骨骼节点
end
function SkillNode:OnDestroy()
end
function SkillNode:SkillStart()
    self.isExecute = false
    self.curDelayTime = 0
    self.time = 0
end
function SkillNode:SkillEnd()
    local TYPE = RedRob.Config.SkillNodeType
    if self.nodeType == TYPE.action then
        self.skill.actor:EnterState(RedRob.Config.ActionState.idle)
    end
end
function SkillNode:Update(dt)
    if not self.skill then return end
    if self.isExecute then return end
    if self.curDelayTime < self.delayTime then
        self.curDelayTime = self.curDelayTime + dt
        return
    end
    local caster = self.skill.actor --施法者
    local TYPE = RedRob.Config.SkillNodeType
    local skillId = self.skill.skillId
    if self.nodeType == TYPE.action then
        self.isExecute = true
        local data = RedRob.PoolMgr.GetStateDataFromPool()
        data.animName = self.action
        data.skillId = self.skill.skillId
        self.skill.actor:EnterState(RedRob.Config.ActionState.attack,data)
    elseif self.nodeType == TYPE.effect then
        self.isExecute = true
        local x,y = RedRob.EffectMgr.GetPos(caster.pos.x,caster.pos.y,self.offsetX,self.offsetY,caster.isLeft)
        local resName = "Prefabs/Skill/"..self.resName
        local eff = RedRob.EffectMgr.CreateEffect(caster:GetId(),resName,self.action,x,y,nil,caster:GetSortingOrder())
        eff:SetSkillId(self.skill.skillId)
    elseif self.nodeType == TYPE.bullet then
        self.isExecute = true
        local target = caster:GetTarget()
        local targetId = 0
        if target then
            targetId = target:GetId()
        end
        local x,y = RedRob.EffectMgr.GetPos(caster.pos.x,caster.pos.y,self.offsetX,self.offsetY,caster.isLeft)
        local resName = "Prefabs/Skill/"..self.resName
        RedRob.EffectMgr.CreateBullet(caster:GetId(),targetId,skillId,resName,self.action,x,y,2,caster:GetSortingOrder())
    end
end